package com.cn.lyl.servlet;

import com.alibaba.fastjson.JSONObject;
import com.cn.lyl.model.UserModel;
import com.cn.lyl.service.UserService;
import com.cn.lyl.service.impl.UserServiceImpl;
import com.cn.lyl.util.Tool;
import sun.net.httpserver.HttpServerImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        UserModel userModel = new UserModel();
        UserService userService = new UserServiceImpl();

       String str = Tool.stringIsNotNull(req.getParameter("username"));
        userModel.setUsername(Tool.stringIsNotNull(req.getParameter("username")));
        userModel.setPassword(Tool.stringIsNotNull(req.getParameter("password")));
        userModel.setCode(Tool.stringIsNotNull(req.getSession().getAttribute("code")));
        userModel.setVerityCode(Tool.stringIsNotNull(req.getParameter("code")));
        String result = userService.checkLogin(userModel);
        JSONObject jsonObject = new JSONObject();
        if(result.equals("success")){
            req.getSession().setAttribute(str,str);
        }
        jsonObject.put("result", result);
        resp.getWriter().println(jsonObject);

    }
}
